﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
using System.Configuration;

namespace ZYOPPC
{
    public static class DataFactory
    {
        private static SqlCeConnection myCeConnection = null;
        /// <summary>
        /// Return one row based one query of ID or Name
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="iID"> -1 when it is useless</param>
        /// <param name="strName">"" when it is useless</param>
        /// <returns></returns>
        /// 

        private static void InitDBConnection()
        {
            String ConnString = @"Data Source=\Storage Card\zydb\iching.sdf";
            myCeConnection = new SqlCeConnection(ConnString);

            try
            {
                myCeConnection.Open();
            }
            catch (Exception e)
            {
                throw e;
            }

        }

        private static void CloseDBConnection()
        {
            myCeConnection.Close();
        }

        public static DataTable ExecuteQuery(String QueryStringFormat,
                                                params String[] QueryParameters)
        {
            DataTable dtReturn = new DataTable();

            if (null == myCeConnection || myCeConnection.State != ConnectionState.Open)
            {
                InitDBConnection();
            }

            SqlCeCommand myCommand = new SqlCeCommand();
            myCommand.CommandType = CommandType.Text;
            myCommand.CommandText = String.Format(QueryStringFormat, QueryParameters);
            myCommand.Connection = myCeConnection;

            SqlCeDataAdapter myDataAdapter = new SqlCeDataAdapter(myCommand);
            myDataAdapter.Fill(dtReturn);

            return dtReturn;
        }

    }
}
